#include <iostream>
using namespace std;
#include "stdTcpService.h"
#include <json-c/json.h>
#define PORT 8089
#define BUFFER_SIZE 32
void registerFunc( StdTcpSocket& client)
{
    char username[BUFFER_SIZE];
    //  fgets(senfBuff,sizeof(senfBuff),stdin);
    cout<<"请输入用户名:";
    scanf("%s",username);
    char password[BUFFER_SIZE];
    cout<<"请输入密码:";
    scanf("%s",password);
    /*创建json对象*/
    json_object *jsonObj = json_object_new_object();
    json_object_object_add(jsonObj,"type",json_object_new_int(1));
    json_object_object_add(jsonObj,"username",json_object_new_string(username));
    json_object_object_add(jsonObj,"password",json_object_new_string(password));
    /*将json对象转换为字符串*/
    const char *jsonStr = json_object_to_json_string(jsonObj);
    client.sendMessage(jsonStr);
    /*接收服务端信息*/
    string recvData;
    client.recvMessage(recvData);
    cout<<"recvData"<<recvData<<endl;
    return;
}
void loginFunc( StdTcpSocket& client)
{
    char username[BUFFER_SIZE];
    //  fgets(senfBuff,sizeof(senfBuff),stdin);
    cout<<"请输入用户名:";
    scanf("%s",username);
    char password[BUFFER_SIZE];
    cout<<"请输入密码:";
    scanf("%s",password);
    /*创建json对象*/
    json_object *jsonObj = json_object_new_object();
    json_object_object_add(jsonObj,"type",json_object_new_int(2));
    json_object_object_add(jsonObj,"username",json_object_new_string(username));
    json_object_object_add(jsonObj,"password",json_object_new_string(password));
    /*将json对象转换为字符串*/
    const char *jsonStr = json_object_to_json_string(jsonObj);
    client.sendMessage(jsonStr);
    /*接收服务端信息*/
    string recvData;
    client.recvMessage(recvData);
    cout<<"recvData"<<recvData<<endl;
    return;
}
int main()
{
    StdTcpSocket client;
    client.connectToServer("192.168.87.128",PORT);
    while (1)
    {
        int choice;
        cout<<"请选择功能："<<endl;
        scanf("%d",&choice);
        if(choice==1)   
        {
            registerFunc(client);
        }
        cout<<"请登录"<<endl;
        if(choice==2)   
        {
            loginFunc(client);
        }
    }
    return 0;
}
